#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);

    this->lport = new QextSerialPort("COM1", QextSerialPort::EventDriven);
    lport->setBaudRate(BAUD9600);
    lport->setFlowControl(FLOW_OFF);
    lport->setParity(PAR_NONE);
    lport->setDataBits(DATA_8);
    lport->setStopBits(STOP_2);
    connect(lport, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    lport->open(QIODevice::ReadWrite);
}

void MainWindow::onReadyRead()
{
    union {
    signed int  ALL;
    struct{
      unsigned char  a;
      unsigned char  b;
    } PART;
    } STRUCT;

    int a = lport->bytesAvailable();
    bytes.resize(a);
    lport->read(bytes.data(), a);

    for(int i=0; i<a; i++)
    {
        Res[KolResiv] = bytes.at(i);
        KolResiv++;
        if(bytes.at(i)=='>')
        {
            STRUCT.PART.b = (((Res[0]-'A')<<4) + (Res[1]-'A')<<0);
            STRUCT.PART.a = (((Res[2]-'A')<<4) + (Res[3]-'A')<<0);
            X = STRUCT.ALL;

            STRUCT.PART.b = (((Res[4]-'A')<<4) + (Res[5]-'A')<<0);
            STRUCT.PART.a = (((Res[6]-'A')<<4) + (Res[7]-'A')<<0);
            Y = STRUCT.ALL;

            STRUCT.PART.b = (((Res[8]-'A')<<4) + (Res[9]-'A')<<0);
            STRUCT.PART.a = (((Res[10]-'A')<<4) + (Res[11]-'A')<<0);
            Z = STRUCT.ALL;


            STRUCT.PART.a = (((Res[12]-'A')<<4) + (Res[13]-'A')<<0);
            STRUCT.PART.b = (((Res[14]-'A')<<4) + (Res[15]-'A')<<0);
            AX = STRUCT.ALL;

            STRUCT.PART.a = (((Res[16]-'A')<<4) + (Res[17]-'A')<<0);
            STRUCT.PART.b = (((Res[18]-'A')<<4) + (Res[19]-'A')<<0);
            AY = STRUCT.ALL;

            STRUCT.PART.a = (((Res[20]-'A')<<4) + (Res[21]-'A')<<0);
            STRUCT.PART.b = (((Res[22]-'A')<<4) + (Res[23]-'A')<<0);
            AZ = STRUCT.ALL;


            ui->lineEdit->setText(QString::number(X));
            ui->lineEdit_2->setText(QString::number(Y));
            ui->lineEdit_3->setText(QString::number(Z));

            ui->lineEdit_4->setText(QString::number(AX));
            ui->lineEdit_5->setText(QString::number(AY));
            ui->lineEdit_6->setText(QString::number(AZ));


            ui->dial->setValue(X);
            ui->dial_2->setValue(Y);
            ui->dial_3->setValue(Z);

            ui->dial_4->setValue(AX);
            ui->dial_5->setValue(AY);
            ui->dial_6->setValue(AZ);

            ui->dial_7->setValue(abs(X)-abs(Y));

//            X = ((Res[0]-0x30)<<12) + ((Res[1]-0x30)<<8) + ((Res[2]-0x30)<<4) + ((Res[3]-0x30)<<0);
//            Y = ((Res[4]-0x30)<<12) + ((Res[5]-0x30)<<8) + ((Res[6]-0x30)<<4) + ((Res[7]-0x30)<<0);
//            Z = ((Res[8]-0x30)<<12) + ((Res[9]-0x30)<<8) + ((Res[10]-0x30)<<4) + ((Res[11]-0x30)<<0);
            KolResiv = 0;
        }
    }
}


MainWindow::~MainWindow()
{
    delete ui;
}
